#include<stdio.h>
#include<stdlib.h>		//产生随机数的头文件
#include<time.h>	
#include<math.h>	
#include<conio.h>		
#include<graphics.h>    //图形库
#define GRID_W 100		//格子的宽度
#define	MAX_SIZE 4	    //每行、列的格子的数量
#define	INTERVAL 15	    //格子之间的距离
#define WIN_SIZE MAX_SIZE*GRID_W+INTERVAL*(MAX_SIZE+1) //矩形窗口的宽度和高度(475,475)
enum Color //枚举格子的颜色
{
	zero = RGB(205, 193, 180),
	To1 = RGB(238, 228, 218),//2的颜色
	To2 = RGB(237, 224, 200),
	To3 = RGB(242, 177, 121),//8的颜色
	To4 = RGB(245, 149, 99),
	To5 = RGB(246, 124, 95),//32的颜色
	To6 = RGB(246, 94, 59),
	To7 = RGB(242, 177, 121),//128的颜色
	To8 = RGB(237, 204, 97),
	To9 = RGB(255, 0, 128),//512的颜色
	To10 = RGB(145, 0, 72),
	To11 = RGB(242, 17, 150),//2048的颜色
	back = RGB(187, 173, 160),//背景颜色
};
Color arr[13] = { zero,To1,To2,To3,To4,To5,To6,To7,To8,To9,To10,To11,back };

//定义数组用来存储数据
int map[MAX_SIZE][MAX_SIZE] = {2,2,2,2};
//产生随机数(2和4)
int createNum()
{
	srand((unsigned)time(NULL) + clock());

	if (rand() % 3 == 0)
	{
		return 4;
	}
	else
	{
		return 2;
	}
}
//初始化数据
void gameInit()
{
	for (int i = 0; i < 2;)
	{
		int r = rand() % MAX_SIZE;
		int c = rand() % MAX_SIZE;
		if (map[r][c] == 0)
		{
			map[r][c] = createNum();
			i++;
		}


	}

}
//界面绘制
void gameDraw()
{	//为窗口填充背景颜色 RGB
	setbkcolor(RGB(100, 100, 100));//灰色
	//清除设备，组合使用函数
	cleardevice();

	//矩形的绘制
	//rectangle(20, 20, 80, 80);//无填充矩形
	//solidrectangle(120,120,180,180);//可填充颜色矩形
	for (int i = 0; i < MAX_SIZE; i++)
	{
		for (int j = 0; j < MAX_SIZE; j++)
		{
			int x = j * GRID_W + INTERVAL * (j + 1);
			int y = i * GRID_W + INTERVAL * (i + 1);
			//为格子填充颜色
			int index = (int)log2((double)map[i][j]);//不同数据不同颜色
			COLORREF tcolor = arr[index];
			setfillcolor(tcolor);
			//绘制圆角格子
			solidroundrect(x, y, x + GRID_W, y + GRID_W, 10, 10);
			//数组里面的数据绘制到图形窗口
			
				//设置文字的属性
				settextstyle(60, 0, "黑体");
				//设置文字背景透明与文字颜色
				setbkmode(1);
				settextcolor(RGB(241, 122, 146));
				//整数转成字符串
				char str[10] = "";
				sprintf_s(str, "%d", map[i][j]);
				//求文本框的高度和宽度
				int tw = textwidth(str);
				int th = textheight(str);
				int tempx = (GRID_W - tw) / 2;
				int tempy = (GRID_W - th) / 2;

				outtextxy(x + tempx, y + tempy, str);
			
			printf("%d", map[i][j]);
		}
		printf("\n");
	}

}
//移动数据
void moveUp()
{
	for (int i = 0; i < MAX_SIZE; i++)
	{
		int temp = 0;
		for (int a = 1; a < MAX_SIZE; a++)
		{
			if (map[a][i] != 0)
			{
				if (map[temp][i] == 0)
				{
					map[temp][i] = map[a][i];
					map[a][i] = 0;
				}
				else if (map[temp][i] == map[a][i]) 
				{
					map[temp][i] += map[a][i];
					map[a][i] = 0;
					temp++;
				}
				else
				{
					map[temp + 1][i] = map[a][i];
					if (temp + 1 != a) 
					{
						map[a][i] = 0;
					}
					temp++;
				}

			}
		}
	}

}
void moveDown()
{
	for (int i = 0; i < MAX_SIZE; i++)
	{
		int temp = MAX_SIZE - 1;
		for (int a = MAX_SIZE - 2  ; a >= -1; a--)
		{
			if (map[a][i] != 0)
			{
				if (map[temp][i] == 0)
				{
					map[temp][i] = map[a][i];
					map[a][i] = 0;
				}else if (map[temp][i] == map[a][i])
				{
					map[temp][i] += map[a][i];
					map[a][i] = 0;
					temp--;
				}
				else
				{
					map[temp - 1][i] = map[a][i];
					if (temp - 1 != a)
					{
						map[a][i] = 0;
					}
					temp--;
				}
			}

		}
	}
}

void moveLeft() 
{
	for (int i = 0; i < MAX_SIZE; i++)
	{
		int temp = 0;
		for (int a = 1; a < MAX_SIZE; a++)
		{
			if (map[i][a] != 0)
			{
				if (map[i][temp] == 0)
				{
					map[i][temp] = map[i][a];
					map[i][a] = 0;
				}
				else if (map[i][temp] == map[i][a])
				{
					map[i][temp] += map[i][a];
					map[i][a] = 0;
					temp++;
				}
				/*else
				{
					map[i][temp + 1] = map[i][a];
					if (temp + 1 != a)
					{
						map[a][i] = 0;
					}
					temp++;
				}*/

			}
		}
	}
}
void moveRigth() 
{
	for (int i = 0; i < MAX_SIZE; i++)
	{
		int temp = MAX_SIZE - 1;
		for (int a = MAX_SIZE - 2; a >= -1; a--)
		{
			if (map[i][a] != 0)
			{
				if (map[i][temp] == 0)
				{
					map[i][temp] = map[i][a];
					map[i][a] = 0;
				}
				else if (map[i][temp] == map[i][a])
				{
					map[i][temp] += map[i][a];
					map[i][a] = 0;
					temp++;
				}
				/*else
				{
					map[i][temp + 1] = map[i][a];
					if (temp + 1 != a)
					{
						map[a][i] = 0;
					}
					temp++;
				}*/

			}
		}
	}
}
//按键处理
void keyDeal()
{
	char key = _getch();
	switch (key)
	{
	case 'w':
	case  'W':
	case 72:
		moveUp();
		break;
	case 's':
	case  'S':
	case 80:
		moveDown();
		break;
	case 'a':
	case  'A':
	case 75:
		//moveLeft;
		break;
	case 'd':
	case  'D':
	case 77:
		//moveRigth();
		break;

	}
}
int main()
{
	initgraph(WIN_SIZE, WIN_SIZE, SHOWCONSOLE);//创建窗口

	gameInit();
	
	while (1)
	{
		gameDraw();
		keyDeal();
	};
	closegraph();//关闭图形窗口
	return 0;
}